/**
 * @author Chris Wilcox
 * @author Preston Parrett
 * 
 * @date 2008/03/17
 * 
 * The Evolution program was written for UCSB's CS165b class.
 * 
 */

package evolution;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.MenuShortcut;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.TextField;
import java.awt.image.*;

@SuppressWarnings("serial")

public class UI extends Frame implements ActionListener, TextListener {

	/*
	 * Run the UI from the command line
	 */
	public static void main(String[] args) {
	      if (args.length >= 1)
	         new UI(null);
	      else
	         new UI(null);
	      
	   }
	
	private God god;
	
	// Set to the AppletLauncher which is running this instance.
	// If run some other way, set to null.
	private final AppletLauncher owner;
	
	private Display display;
	private Stats stats;
	
	private TextField speed;
	private TextField year;
	
	/**
	 * Construct the application UI.
	 * @param owner the AppletLauncher that started this UI or null otherwise
	 */
	public UI(AppletLauncher owner) {
		
		super("Evolution (Alpha)");
		this.owner = owner;
		
		god = new God();
		
		/////////////////////////
		// Add Display Components
		/////////////////////////
		this.setResizable(true);
		this.setSize(770,600);

		display = new Display();
		display.setSize(getWidth(),getHeight());
		display.setLayout(new FlowLayout(FlowLayout.LEFT));
		display.setBackground(new Color(230,230,230));
		this.add(display);
		
		god.createWorld();
		display.add(god.getWorld());
		
		stats = new Stats();
		stats.setBackground(new Color(240,255,255));
		display.add(stats);
		
		display.add(new Label("Wait Time:"));		
		speed = new TextField("200");
		speed.addTextListener(this);
		display.add(speed);
		
		
		display.add(new Label("Year Length:"));	
		year = new TextField("1000");
		year.addTextListener(this);
		display.add(year);		
		
		/////////////////////////
		// Build the Menubar
		/////////////////////////
		MenuBar menubar = new MenuBar();
		
			// World Menu
			Menu worldMenu = new Menu("World");
				MenuItem _new = new MenuItem("New World", new MenuShortcut('N'));
			    _new.setActionCommand("new");
			    worldMenu.add(_new);
			    worldMenu.addActionListener(this);
		    menubar.add(worldMenu);
		
			// Control Menu
			Menu controlMenu = new Menu("Control");
			    MenuItem pause = new MenuItem("Pause", new MenuShortcut('P'));
			    pause.setActionCommand("pause");
			    controlMenu.add(pause);
			    MenuItem start = new MenuItem("Start", new MenuShortcut('S'));
			    pause.setActionCommand("pause");
			    controlMenu.add(start);
				controlMenu.addActionListener(this);
		    menubar.add(controlMenu);
	    
		setMenuBar(menubar);
		
		//////////////////////////////
		// Listen for window actions
		//////////////////////////////
		addWindowListener( new WindowAdapter() {
            public void windowClosing(WindowEvent evt) {
               close();
            }
            public void windowOpened(WindowEvent evt) {
               //world.create();
            }
         });
		
		
		/////////////////////////
		// Finish up GUI init
		/////////////////////////
		this.setVisible(true);

		setComponentSize(god.getWorld(),500,500);
		setComponentSize(stats,200,500);
		
		validate();
		
		////// Start the New World! //////
		god.start();
	}
	
	
	public void actionPerformed(ActionEvent a) {
		
		if      (a.getActionCommand().equalsIgnoreCase("pause")) 
		{
			god.pause();
		}
		else if (a.getActionCommand().equalsIgnoreCase("new"))  
		{
			Stats.resetAll();
			display.remove(god.getWorld());
			display.add(god.createWorld());
			god.getWorld().setLocation(5,5);
			setComponentSize(god.getWorld(),500,500);
			repaint();
		}
		else if (a.getActionCommand().equalsIgnoreCase("start"))  
		{
			god.start();
		}
	}
	
	public void textValueChanged(TextEvent te) {
		
		if (te.getSource() == speed) 
		{
			if (speed.getText() == null) return;
			
			int speedInput = 0;
			
			try {
				speedInput = Integer.parseInt(speed.getText());
			}
			catch (NumberFormatException n) {
				return;
			}
			
			god.tickLength(speedInput);
		}
		
		if (te.getSource() == year) 
		{
			if (year.getText() == null) return;
			
			int yearInput = 0;
			
			try {
				yearInput = Integer.parseInt(year.getText());
			}
			catch (NumberFormatException n) {
				return;
			}
			
			god.yearLength(yearInput);
		}
		
	}
	

	
	
	private void setComponentSize(Component c, int width, int height) {
		c.setSize(width,height);
		c.setPreferredSize(new Dimension(width,height));
	}
	
	
	public void componentsPrint() {
		for (Component c : display.getComponents()) {
			System.out.println(c);
		}
	}
	
	
	
	void close() {
		// stop any stuff running
		
		if (owner == null)  // running as an application
	        System.exit(0);
	    else
	        owner.frameHasClosed();
	}

}
